package com.example.common;

import lombok.Data;

import java.io.Serializable;

/**
 * <p> 通用返回结果 </p>
 *
 * @author Chris
 * @since 2022/10/31 14:45
 */
@Data
public class CommonResp<T> implements Serializable {

    private static final long serialVersionUID = -2151363532646463749L;

    public static final int SUCCESS = 0;
    public static final String SUCCESS_MSG = "request success";

    public static final int FAIL = -1;
    public static final String FAIL_MSG = "request fail";

    private Integer code = SUCCESS;

    private String msg;

    private T data;

    public static <T> CommonResp<T> success() {
        return success(null, SUCCESS, SUCCESS_MSG);
    }

    public static <T> CommonResp<T> success(T data) {
        return success(data, SUCCESS, SUCCESS_MSG);
    }

    public static <T> CommonResp<T> success(String msg) {
        return success(null, SUCCESS, msg);
    }

    public static <T> CommonResp<T> success(T data, String msg) {
        return success(data, SUCCESS, msg);
    }

    public static <T> CommonResp<T> success(T data, Integer code, String msg) {
        final CommonResp<T> commonResp = new CommonResp<>();
        commonResp.setCode(code);
        commonResp.setMsg(msg);
        commonResp.setData(data);
        return commonResp;
    }

    public static <T> CommonResp<T> fail(String msg) {
        return fail(null, FAIL, msg);
    }

    public static <T> CommonResp<T> fail(Integer code, String msg) {
        return fail(null, code, msg);
    }

    public static <T> CommonResp<T> fail(T data, Integer code, String msg) {
        final CommonResp<T> commonResp = new CommonResp<>();
        commonResp.setCode(code);
        commonResp.setMsg(msg);
        commonResp.setData(data);
        return commonResp;
    }

}